package com.football.springboot.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.football.springboot.vo.PlayerDataQueryVo;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.football.springboot.common.Return;
import com.football.springboot.common.Result;
//导入service
import com.football.springboot.service.IPlayerDataService;
//导入entity
import com.football.springboot.entity.PlayerData;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李越
 * @since 2023-03-03
 */

@Api(tags = "球员数据管理")
@RestController
@RequestMapping("/playerdata")
public class PlayerDataController {

    @Resource
    private IPlayerDataService playerDataService;

    // 新增或者更新
    @PostMapping("/saveOrUpdate")
    public Result save(@RequestBody PlayerData playerData) {
        playerDataService.saveOrUpdate(playerData);
        return Result.success();
    }


    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        playerDataService.removeById(id);
        return Result.success();
    }

    //批量删除
    @PostMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        playerDataService.removeByIds(ids);
        return Result.success();
    }

    //查询全部
    @GetMapping("/findall")
    public Result findAll() {
        return Result.success(playerDataService.list());
    }

    //根据id查找
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(playerDataService.getById(id));
    }

    //分页查询
    @GetMapping("/pagePlayerData")
    public Return pagePlayerData(@RequestParam Integer current,
                                 @RequestParam Integer limit,
                                 PlayerDataQueryVo playerDataQueryVo) {

        //创建page对象,传递当前页数current,每页记录数limit
        Page<PlayerData> page = new Page<>(current,limit);
        //构建条件 模糊查询
        QueryWrapper<PlayerData> queryWrapper = new QueryWrapper<>();

        String leagueSeason = playerDataQueryVo.getLeagueSeason();
        Integer teamId=playerDataQueryVo.getTeamId();
        Integer leagueId=playerDataQueryVo.getLeagueId();
        Integer playerId=playerDataQueryVo.getPlayerId();

        //赛季查询
        queryWrapper.eq(!StringUtils.isEmpty(leagueSeason),"league_season",leagueSeason);
        //联赛查询
        queryWrapper.eq(leagueId!=null,"league_id",leagueId);
        //球队查询
        queryWrapper.eq(teamId!=null,"team_id",teamId);
        // 球员查询
        queryWrapper.eq(playerId!=null,"player_id",playerId);



        queryWrapper.orderByDesc("player_id","team_id","league_id");
        Page<PlayerData> pagePlayerData = playerDataService.page(page,queryWrapper);
        List<PlayerData> records = pagePlayerData.getRecords();
        records.forEach(playerData -> {
            //将球队信息插入表中
            playerData.setTeam(playerDataService.getteaminfo(playerData.getTeamId()));
            //将球员信息插入表中
            playerData.setPlayer(playerDataService.getplayerinfo(playerData.getPlayerId()));
            //将联赛信息插入表中
            playerData.setLeague(playerDataService.getleagueinfo(playerData.getLeagueId()));
        });
        return Return.ok().put("pagePlayerData",pagePlayerData).put("total",pagePlayerData.getTotal());
    }

}

